#include<pthread.h>
#include <iostream>
#include <string> 
#include <mutex>
#include <unistd.h>
using namespace std;
pthread_mutex_t mtx=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
void *threatRun(void * args)
{
    string name=static_cast<const char*>(args);
    while(true)
    {
    pthread_mutex_lock(&mtx);
    pthread_cond_wait(&cond,&mtx);
    cout<<name<<endl;
    pthread_cond_broadcast(&cond);
    pthread_mutex_unlock(&mtx);
    }
}
int main()
{
    pthread_t pid[2];
    for(int i=0;i<2;i++)
    {
        char* buf=new char[64];
        if(i==0)
        snprintf(buf,64,"我是线程-%c",'A');
        else
        snprintf(buf,64,"我是线程-%c",'B');
        pthread_create(pid+i,nullptr,threatRun,buf);
    }
    while(true)
    {
        // cout<<"mian wake-up"<<endl;
        pthread_cond_broadcast(&cond);
        sleep(1);
    }
    for(int i=0;i<2;i++)
    {
        pthread_join(pid[i],nullptr);
    }
    return 0;
}
